package com.rajesh.messageservice.helper;

import android.content.Context;
import android.content.SharedPreferences;

import java.lang.reflect.Field;

/**
 * Shared Preferences 工具类， 便于读写实体对象
 */
public class PreferenceHelper {
    private final String TAG = "PreferenceHelper";
    private Context context = null;
    private String identity = null;

    public PreferenceHelper(Context context, String identity) {
        this.context = context;
        this.identity = identity;
    }

    /**
     * 将实体对象数据存储到 SharedPreferences 中
     *
     * @author Rajesh
     * @param object 数据对象
     * */
    public void put(Object object) {
        Field[] fields = object.getClass().getDeclaredFields();
        Field.setAccessible(fields, true);

        try {
            SharedPreferences preferences = context.getSharedPreferences(identity, Context.MODE_PRIVATE);
            SharedPreferences.Editor editor = preferences.edit();

            for (Field field : fields) {
                if (field.getName().equals("$change")) continue;

                Object value = field.get(object);
                String type = field.getType().getSimpleName();

                if ("String".equals(type)) {
                    editor.putString(field.getName(), (String) value);
                } else if ("Integer".equals(type) || "int".equals(type)) {
                    editor.putInt(field.getName(), (Integer) value);
                } else if ("Boolean".equals(type) || "boolean".equals(type)) {
                    editor.putBoolean(field.getName(), (Boolean) value);
                } else if ("Float".equals(type) || "float".equals(type)) {
                    editor.putFloat(field.getName(), (Float) value);
                } else if ("Long".equals(type) || "long".equals(type)) {
                    editor.putLong(field.getName(), (Long) value);
                }
            }
            editor.commit();
        } catch (IllegalAccessException e) {
            e.printStackTrace();
        }
    }

    /**
     * 从 SharedPreferences 中获取实体对象数据
     *
     * @author Rajesh
     * @param c 取出来的实体数据类型
     * @return 数据对象
     * */
    public <T extends Object> T get(Class c) {

        T t = null;
        try {
            t = (T) c.newInstance();
            Field[] fields = c.getDeclaredFields();
            Field.setAccessible(fields, true);
            SharedPreferences preferences = context.getSharedPreferences(identity, Context.MODE_PRIVATE);

            for (Field field : fields) {
                if (field.getName().equals("$change")) continue;
                String type = field.getType().getSimpleName();

                if ("String".equals(type)) {
                    field.set(t, preferences.getString(field.getName(), ""));
                } else if ("Integer".equals(type) || "int".equals(type)) {
                    field.set(t, preferences.getInt(field.getName(), 0));
                } else if ("Boolean".equals(type) || "boolean".equals(type)) {
                    field.set(t, preferences.getBoolean(field.getName(), false));
                } else if ("Float".equals(type) || "float".equals(type)) {
                    field.set(t, preferences.getFloat(field.getName(), 0.0f));
                } else if ("Long".equals(type) || "long".equals(type)) {
                    field.set(t, preferences.getLong(field.getName(), 0l));
                }
            }
        } catch (IllegalAccessException e) {
            e.printStackTrace();
        } catch (InstantiationException e) {
            e.printStackTrace();
        }
        return t;
    }
}
